package com.linghui.videoplus.ipai.activity;


import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.TextView;

import com.linghui.videoplus.ipai.R;
import com.linghui.videoplus.ipai.media.Player;

public class PlayerActivity extends BaseActivity implements OnTouchListener,OnCompletionListener {
	private SurfaceView surfaceView;
	private ImageView ctrlImageView;
	private ImageView shareImageView;
	private SeekBar progressSeekBar;

	private Player player;
	private LinearLayout layoutProgress;
	private LinearLayout loadingLL;
	private RelativeLayout playRelativeLayout;
	private TextView loadTextView;
//	private AudioManager mAudioManager = null;
//	private VerticalSeekBar volSeekBar;
//	private int maxVolume = 0;
//	private int currentVolume = 0;

	private String url = null;
	private String detail = null;

	private String title = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
		this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
		setContentView(R.layout.activity_player);
		
		surfaceView = (SurfaceView) this.findViewById(R.id.surfaceView1);
		layoutProgress = (LinearLayout) this.findViewById(R.id.layoutProgress);
		loadingLL=(LinearLayout)findViewById(R.id.loadingLL);
		
		
		playRelativeLayout = (RelativeLayout) this.findViewById(R.id.playRelativeLayout);
		playRelativeLayout.setVisibility(View.GONE);
		
		layoutProgress.setBackgroundColor(Color.parseColor("#33FFFFFF"));

		loadTextView = (TextView) findViewById(R.id.ProgressBarTextView);

		ctrlImageView = (ImageView) findViewById(R.id.ctrImageView);
		ctrlImageView.setOnTouchListener(this);

		shareImageView= (ImageView) findViewById(R.id.shareImageView);
		shareImageView.setOnTouchListener(this);
		progressSeekBar = (SeekBar) this.findViewById(R.id.skbProgress);
		progressSeekBar.setOnSeekBarChangeListener(new ProgressSeekBarChangeEvent());
		url = getIntent().getStringExtra("URL");
		detail = getIntent().getStringExtra("detail");
		title = getIntent().getStringExtra("name");
		player = new Player(this, surfaceView, progressSeekBar, url,
				loadingLL);
	}

	@Override
	protected void onStop() {
		if (player.mediaPlayer != null && player.isPlay()) {
			player.pause();
		}
		super.onStop();
	}
	
	@Override
	public boolean onTouch(View v, MotionEvent event) {
		if(v==shareImageView){
			if (event.getAction() == MotionEvent.ACTION_DOWN) {
				shareImageView.setBackgroundResource(R.drawable.btn_share_play_down);
			}else if (event.getAction() == MotionEvent.ACTION_UP) {
				shareImageView.setBackgroundResource(R.drawable.btn_share_play_up);
				String content="精彩微视频："+title+":"+detail+"；分享来自@看看微视频。";
				Intent intent = new Intent(Intent.ACTION_SEND);
				intent = new Intent(Intent.ACTION_SEND);
				intent.setType("text/plain");
				intent.putExtra(Intent.EXTRA_SUBJECT, "分享");
				intent.putExtra(Intent.EXTRA_TEXT, content);
				intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				startActivity(Intent.createChooser(intent, getTitle()));
			}
			
		}else if(v==ctrlImageView){
			if (event.getAction() == MotionEvent.ACTION_DOWN) {
				
			}else if (event.getAction() == MotionEvent.ACTION_UP) {
				if(player.isPlay()){
					player.pause();
					ctrlImageView.setImageResource(R.drawable.btn_main_play_up);
				}else {
					player.play();
					ctrlImageView.setImageResource(R.drawable.btn_pause);
				}
			}
		}
		return true;
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		if (event.getAction() == MotionEvent.ACTION_UP) {
			if (playRelativeLayout.getVisibility() == View.VISIBLE) {
				playRelativeLayout.setVisibility(View.GONE);

			} else if (playRelativeLayout.getVisibility() == View.GONE) {
				playRelativeLayout.setVisibility(View.VISIBLE);
			}
		}
		return true;
	}

	@Override
	public void onCompletion(MediaPlayer mp) {
		ctrlImageView.setImageResource(R.drawable.btn_main_play_up);
	}
	
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_HOME) {
			player.pause();
		}
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setMessage("您确定退出观看吗?");
			builder.setTitle("提示");
			builder.setCancelable(false);
			builder.setPositiveButton("确定",
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int id) {
							if (player.isPlay()) {
								player.stop();
							}
							PlayerActivity.this.setResult(RESULT_OK, getIntent());
							PlayerActivity.this.finish();
						}
					});
			builder.setNegativeButton("取消",
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int id) {
							dialog.cancel();
						}
					});
			AlertDialog alert = builder.create();
			alert.show();
		}
		return super.onKeyDown(keyCode, event);
	}


	public static void launch(Activity a, String url, final String name,final String detail,
			final boolean record) {
		Intent intent = new Intent(a, PlayerActivity.class);
		intent.putExtra("URL", url);
		intent.putExtra("name", name);
		intent.putExtra("detail", detail);
		intent.putExtra("record", record);
		a.startActivity(intent);
	}


	/**
	 * 播放器进度条
	 * 
	 * @author chenxf
	 * 
	 */
	class ProgressSeekBarChangeEvent implements SeekBar.OnSeekBarChangeListener {
		int progress;

		@Override
		public void onProgressChanged(SeekBar seekBar, int progress,
				boolean fromUser) {
			this.progress = progress * player.mediaPlayer.getDuration()
					/ seekBar.getMax();
		}

		@Override
		public void onStartTrackingTouch(SeekBar seekBar) {
		}

		@Override
		public void onStopTrackingTouch(SeekBar seekBar) {
			// seekTo()的参数是相对与影片时间的数字，
			// 而不是与seekBar.getMax()相对的数字
			player.mediaPlayer.seekTo(progress);
		}
	}

}
